package com.lzb.compressmaster.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lzb.compressmaster.BaseAc;
import com.lzb.compressmaster.Content;
import com.lzb.compressmaster.HomeActivity;
import com.lzb.compressmaster.R;
import com.lzb.compressmaster.adapter.CompressFormatAdapter;
import com.lzb.compressmaster.bean.CompressFormatBean;
import com.lzb.compressmaster.databinding.ActivityFileCompressBinding;
import com.lzb.compressmaster.dialog.CompressDialog;
import com.lzb.compressmaster.dialog.RenameDialog;
import com.lzb.compressmaster.utils.ApacheCompressUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FileCompressActivity extends BaseAc<ActivityFileCompressBinding> implements OnItemClickListener, View.OnClickListener {

    public static List<String> listPath;
    private CompressFormatAdapter formatAdapter;
    private String fileName;
    private ApacheCompressUtil.CompressMode compressMode;
    private ApacheCompressUtil.CompressFormat compressFormat;
    private int oldPosition;

    @Override
    protected int onCreate() {
        return R.layout.activity_file_compress;
    }

    @Override
    protected void initView() {
        mDataBinding.ivFileCompressBack.setOnClickListener(v -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.tvFileName.setOnClickListener(this);
        mDataBinding.tvModeSpeed.setOnClickListener(this);
        mDataBinding.tvModeVolume.setOnClickListener(this);
        mDataBinding.tvStartCompress.setOnClickListener(this);

        mDataBinding.rvFormatList.setLayoutManager(new GridLayoutManager(mContext, 6));
        formatAdapter = new CompressFormatAdapter();
        mDataBinding.rvFormatList.setAdapter(formatAdapter);
        formatAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tvFileName) {//文件名
            showRenameDialog();
        } else if (id == R.id.tvModeSpeed) {//速度快
            clearSelection();
            mDataBinding.tvModeSpeed.setBackgroundResource(R.drawable.compress_mode_sel);
            mDataBinding.tvModeSpeed.setTextColor(Color.parseColor("#FFFFFF"));
            compressMode = ApacheCompressUtil.CompressMode.FAST;
        } else if (id == R.id.tvModeVolume) {//体积小
            clearSelection();
            mDataBinding.tvModeVolume.setBackgroundResource(R.drawable.compress_mode_sel);
            mDataBinding.tvModeVolume.setTextColor(Color.parseColor("#FFFFFF"));
            compressMode = ApacheCompressUtil.CompressMode.SMALL;
        } else if (id == R.id.tvStartCompress) {//开始压缩
            showCompressDialog();
        }
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        formatAdapter.getItem(oldPosition).setSelected(false);
        oldPosition = position;
        formatAdapter.getItem(position).setSelected(true);
        formatAdapter.notifyDataSetChanged();
        compressFormat = formatAdapter.getItem(position).getFormat();
    }

    @Override
    protected void initData() {
        compressMode = ApacheCompressUtil.CompressMode.FAST;
        List<CompressFormatBean> list = new ArrayList<>();
        list.add(new CompressFormatBean(ApacheCompressUtil.CompressFormat.ZIP, true));
        list.add(new CompressFormatBean(ApacheCompressUtil.CompressFormat.SEVEN_Z, false));
        list.add(new CompressFormatBean(ApacheCompressUtil.CompressFormat.TAR, false));
        list.add(new CompressFormatBean(ApacheCompressUtil.CompressFormat.GZIP, false));
        list.add(new CompressFormatBean(ApacheCompressUtil.CompressFormat.BZIP2, false));
        list.add(new CompressFormatBean(ApacheCompressUtil.CompressFormat.XZ, false));
        list.add(new CompressFormatBean(ApacheCompressUtil.CompressFormat.AR, false));
        list.add(new CompressFormatBean(ApacheCompressUtil.CompressFormat.CPIO, false));
        list.add(new CompressFormatBean(ApacheCompressUtil.CompressFormat.JAR, false));
        compressFormat = list.get(0).getFormat();
        formatAdapter.setList(list);
        fileName = getString(R.string.compress_package) + TimeUtils.getNowMills();
        mDataBinding.tvFileName.setText(fileName);
        mDataBinding.tvFileSize.setText(listPath.size() + getString(R.string.ge_unit));
    }

    private void startCompress() {
        String path = PathUtils.getExternalDocumentsPath() + Content.myCompressFile + "/" + fileName + ApacheCompressUtil.getExtension(compressFormat);
        ApacheCompressUtil.compressFilesFromPaths(mContext, listPath, new File(path), compressFormat, compressMode, new ApacheCompressUtil.CompressCallback() {
            @Override
            public void onProgress(int progress) {
                showDialog(getString(R.string.compress_ing));
            }

            @Override
            public void onSuccess(String outputPath) {
                dismissDialog();
                ToastUtils.showShort(getString(R.string.compress_suc));
                finish();
            }

            @Override
            public void onError(String error) {
                dismissDialog();
                ToastUtils.showShort(error);
            }
        });
    }

    private void showRenameDialog() {
        RenameDialog renameDialog = new RenameDialog(mContext);
        renameDialog.setName(fileName);
        renameDialog.setListener(new RenameDialog.Ilistener() {
            @Override
            public void clickRename(String name) {
                fileName = name;
            }
        });
        renameDialog.show();
    }

    private void showCompressDialog() {
        CompressDialog compressDialog = new CompressDialog(mContext);
        compressDialog.setCompress(true);
        compressDialog.setListener(new CompressDialog.Ilistener() {
            @Override
            public void clickConfirm() {
                startCompress();
            }
        });
        compressDialog.show();
    }

    private void clearSelection() {
        mDataBinding.tvModeSpeed.setBackgroundResource(R.drawable.compress_mode_bg);
        mDataBinding.tvModeSpeed.setTextColor(Color.parseColor("#333333"));
        mDataBinding.tvModeVolume.setBackgroundResource(R.drawable.compress_mode_bg);
        mDataBinding.tvModeVolume.setTextColor(Color.parseColor("#333333"));
    }

    @Override
    public FrameLayout getNativeView() {
        return mDataBinding.adContainer;
    }
}